package blipappeal.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class FileUtil
{
	public static String getStringFromStream(InputStream isData) throws IOException
  {
    ByteArrayOutputStream baosData = new ByteArrayOutputStream();
    
    byte[] abyteBuffer = new byte[1024];
    
    int nBytesRead = 0; 
    while ((nBytesRead = isData.read(abyteBuffer)) >= 0)
    {
      baosData.write(abyteBuffer, 0, nBytesRead);
    }
    baosData.close();
    
    return baosData.toString();
  }
	
	
	public static void close(OutputStream osDest)
  {
    try
    {
      if (osDest != null)
      {
        osDest.close();
      }
    }
    catch(IOException exIO)
    {
      exIO.printStackTrace();
    }
  }
  
  public static void close(InputStream isSrc)
  {
    try
    {
      if (isSrc != null)
      {
        isSrc.close();
      }
    }
    catch(IOException exIO)
    {
      exIO.printStackTrace();
    }
  }
  
  public static void close(Reader reader)
  {
    try
    {
      if (reader != null)
      {
        reader.close();
      }
    }
    catch(IOException exIO)
    {
      exIO.printStackTrace();
    }
  }
  
  public static void close(Writer writer)
  {
    try
    {
      if (writer != null)
      {
        writer.close();
      }
    }
    catch(IOException exIO)
    {
      exIO.printStackTrace();
    }
  }
  
  public static String normalizeFolderPath(String strPath)
  {
    String strNormalizedPath = strPath.replace(File.separatorChar, '/');
    if (!strNormalizedPath.endsWith("/"))
    {
       strNormalizedPath += "/";
    }
    
    return strNormalizedPath;
  }
  
  public static String normalizePath(String strPath)
  {
    return strPath.replace(File.separatorChar, '/');
  }


}
